﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class Management_HighJob : System.Web.UI.Page
{
    int _page = 0;
    int _take = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Bind(0);
        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        var _btn = sender as Button;
        var _id = string.IsNullOrEmpty(_btn.CommandArgument) ? 0 : Convert.ToInt32(_btn.CommandArgument);
        using (var _adc = DBHelper.GetDataContext())
        {
            var _job = _adc.HighJob.FirstOrDefault(j => j.ID == _id);
            if (_job != null)
            {
                 txt_Title.Text=_job.Title;
                 txt_Company.Text=_job.CompanyName;
                 txt_Content.Value=_job.Content;
                 txt_Position.Text=_job.Position;
                 txt_ID.Value = _job.ID.ToString();
            }
            else
            {
                Response.Redirect(Request.Url.ToString());
            }
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            var _btn = sender as Button;
            var _id = string.IsNullOrEmpty(_btn.CommandArgument) ? 0 : Convert.ToInt32(_btn.CommandArgument);
            using (var _adc = DBHelper.GetDataContext())
            {
                var _job = _adc.HighJob.FirstOrDefault(j => j.ID == _id);
                if (_job != null)
                {
                    _adc.HighJob.DeleteOnSubmit(_job);
                    _adc.SubmitChanges();
                }
            }
            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _id = string.IsNullOrEmpty(txt_ID.Value) ? 0 : Convert.ToInt32(txt_ID.Value);
                var _job = _adc.HighJob.FirstOrDefault(j => j.ID == _id);
                if (_job == null)
                {
                    _job = new HighJob();

                    _job.Title = txt_Title.Text;
                    _job.CompanyName = txt_Company.Text;
                    _job.Content = txt_Content.Value;
                    _job.Position = txt_Position.Text;
                    _job.PublishDate = DateTime.Now;

                    _adc.HighJob.InsertOnSubmit(_job);
                }
                else
                {
                    _job.Title = txt_Title.Text;
                    _job.CompanyName = txt_Company.Text;
                    _job.Content = txt_Content.Value;
                    _job.Position = txt_Position.Text;
                    _job.PublishDate = DateTime.Now;
                }
                _adc.SubmitChanges();
                Response.Redirect(Request.Url.ToString());
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }

    }
    protected void Bind(int _page)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                lbl_Current.Text = (_page + 1).ToString();
                lbl_Total.Text = (_adc.HighJob.Count() / _take + 1).ToString();
                gv_HighJob.DataSource = _adc.HighJob.OrderByDescending(h => h.PublishDate).Skip(_take * _page).Take(_take);
                gv_HighJob.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "出错:" + _e.Message;
        }
    }
    protected void btn_Up_Click(object sender, EventArgs e)
    {
        _page = Convert.ToInt32(lbl_Current.Text) - 1;
        if (_page > 0) _page -= 1;
        this.Bind(_page);
    }
    protected void btn_Down_Click(object sender, EventArgs e)
    {
        _page = Convert.ToInt32(lbl_Current.Text) - 1;
        if (_page < Convert.ToInt32(lbl_Total.Text) - 1) _page += 1;
        this.Bind(_page);
    }
    protected void btn_Goto_Click(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(txt_Page.Text)) _page = Convert.ToInt32(txt_Page.Text) - 1;
        }
        catch { }
        this.Bind(_page);
    }
}
